var map;

//��ʼ��
function initMap(){
	map = new BMap.Map("bd-map");             
	var point = new BMap.Point(116.404, 39.915);  
	map.centerAndZoom(point, 15);                 
	map.addControl(new BMap.NavigationControl());    
	//map.addControl(new BMap.ScaleControl());    
	//map.addControl(new BMap.OverviewMapControl());    
	map.setCurrentCity("北京");
	map.enableScrollWheelZoom(true);   
	
	enableSelectPointLocation();
	enableAutoInput();
}

//�Զ���λ
function autoLocation(){
	var geolocation = new BMap.Geolocation();
	geolocation.getCurrentPosition(function(r){
		if(this.getStatus() == BMAP_STATUS_SUCCESS){
			var mk = new BMap.Marker(r.point);
			map.addOverlay(mk);
			map.panTo(r.point);
		}
		else {
			alert('failed'+this.getStatus());
		}
	},{enableHighAccuracy:true}); 
}

function G(id) {
	return document.getElementById(id);
}

//选点定位
function enableSelectPointLocation(){
	var geocoder = new BMap.Geocoder();//���ַ����
	var markers = new Array();
	markers[0]=null;markers[1]=null;
	function showInfo(e){
		var point = e.point;
		
		markers[0] = markers[1];
	    markers[1] = new BMap.Marker(new BMap.Point(point.lng, point.lat));
	    if((markers[1]!=null))
	    	map.addOverlay(markers[1]);
	    if(markers[0] != null)
			map.removeOverlay(markers[0]);
		geocoder.getLocation(point, function(rs){
			var addComp = rs.addressComponents;	
			var title = addComp.street;
			var address = ("地址:"+addComp.city + addComp.district+addComp.street +addComp.streetNumber);
			var infoWindowOptinos = {
				  width : 200,     
				  height: 100,     
				  title : title, 
				  enableMessage:true
			};
			//
			var url = "findneargoods.action?r=10"+"&location="+point.lng+":"+point.lat;
			console.log("位置 "+point.lng+":"+point.lat);
			var goodslistlength;
			$.getJSON(url, function(data) {
				goodslistlength=data.length;
			}); 
			//
			
			
			var content = [];
			content.push('<div>');
			content.push(address+'<br>');
			if(isNaN(goodslistlength)){  
				content.push('附近没有商品');
			}else{
				content.push('<a href="#">共有'+goodslistlength+'件商品点击查看</a>');
			}
			
			content.push('</div>');
			//var info = new BMap.InfoWindow(html.join(""), {offset:new BMap.Size(0,-23),width: 220});
			//var infoWindow = new BMap.InfoWindow(address, infoWindowOptinos);  // ������Ϣ���ڶ��� 
//			var infoWindow = new BMap.InfoWindow(html.join(""), {offset:new BMap.Size(0,-23),width: 220});
			var infoWindow = new BMap.InfoWindow(content.join(""),infoWindowOptinos );
			map.openInfoWindow(infoWindow,point);
		}); 
 
	}
	map.addEventListener("click", showInfo);
	
}
var myValue;
function enableAutoInput(){
	var ac = new BMap.Autocomplete({
		     "input" : "searchKeywords",
			 "location" : map
	});
	ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
		var str = "";
		var _value = e.fromitem.value;
		var value = "";
		if (e.fromitem.index > -1) {
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
		
		value = "";
		if (e.toitem.index > -1) {
			_value = e.toitem.value;
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
		G("searchResultPanel").innerHTML = str;
	});

	ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
		var _value = e.item.value;
		myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
		//setPlace();
	});
}

function setPlace(){
	map.clearOverlays();    //清除地图上所有覆盖物
	function myFun(){
		var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
		map.centerAndZoom(pp, 18);
		map.addOverlay(new BMap.Marker(pp));    //添加标注
	}
	var local = new BMap.LocalSearch(map, { //智能搜索
	  onSearchComplete: myFun
	});
	local.search(myValue);
}